/**
 * Copyright 2011 Micro_SNS Group. All Right reserved.
 */
package com.team.sns.web;

import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.interceptor.SessionAware;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.team.sns.annotations.Permission;
import com.team.sns.entities.Gender;
import com.team.sns.entities.MsgInfo;
import com.team.sns.entities.User;
import com.team.sns.entities.UserRelation;
import com.team.sns.services.MessageService;
import com.team.sns.services.UserFollowService;
import com.team.sns.services.UserService;

/**
 * @author seven zhao
 * @version 1.0
 * 功能说明：前台用户信息管理
 *
 */
@SuppressWarnings("serial")
@Controller @Scope("prototype")
public class UserAction  extends BasicAction implements SessionAware {
	private static Log log = LogFactory.getLog(UserAction.class);
	
	@Resource(name="userServiceBean")
	private UserService userService;
	
	@Resource(name = "userFollowServiceImpl")
	private UserFollowService userFollowService;
	
	@Resource(name = "messageServiceImpl")
	private MessageService messageService;
	
	private Map<Object,Object> session;
	
	private User user;
	
	private UserRelation userRelation;
	
	private MsgInfo info ;
	
	public UserRelation getUserRelation() {
		return userRelation;
	}

	public void setUserRelation(UserRelation userRelation) {
		this.userRelation = userRelation;
	}

	public String followUser(){
		userRelation.setType(1);
		this.userFollowService.save(userRelation);
		return "";
	}
	
	
	
	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public String login() throws Exception {
		User u = userService.login(user);
		if(u == null) {
			return "loginFailed";
		}
		userService.getUserPermissions(u);
		session.put("user", u);
		return "loginSuccess";
	}
	
	@Permission(action="user.action",module="test")
	public String test1(){
		return "loginSuccess";
	}
	
	@Permission(action="user.action",module="test")
	public String test2(){
		return "loginSuccess";
	}
	
	/**
	 * 说明：用户注册
	 * @return
	 * @throws Exception
	 */
	
	public String register() throws Exception {
		user.setGender(Gender.UNSPECIFIED);
		userService.save(user);
		log.debug("用户"+user.getUsername()+"注册成功");
		return "registerSuccess";
	}
	
	/**
	 * return json字符串，内容是message
	 * 说明：判断用户名是否存在
	 * false1:用户名不允许为空
	 * false2:该用户名已经存在
	 * true：该用户名可以进行注册
	 */
	public String checkUsername() throws Exception {
		String message = "";
		boolean flag = false;
		if(user.getUsername()!=null&&"".equals(user.getUsername())){
			flag = userService.chkUsername(user.getUsername().trim());
			if(flag){
				message = "false2";
			}else{
				message = "true";
			}
		}else{
			message = "false1";
		}
		
		return null;
	}
	/**
	 * 说明：修改用户信息
	 * @return
	 * @throws Exception
	 */
	public String editInfo() throws Exception {
		
		log.debug("用户信息修改成功！");
		return null;
	}
	

	public void setSession(Map session) {
		this.session = session;
	}
}
